const express = require('express')
const bodyParser = require('body-parser')
const sendErr = require('./middleware/sendErr')

const router = express.Router()

// 切换城市
router.post('/change/city', bodyParser.json(), async (req, res) => {
  const { city } = req.body || {}
  if (city.region_code) {
    if (req.session.city) {
      req.session.city.city = city
    }
    res.json({
      success: true,
      data: city
    })
  } else {
    res.json({
      success: true
    })
  }
})

// 错误处理
router.post('/sendErr', bodyParser.json(), (req, res) => {
  const { text, title } = req.body
  sendErr(title, text)
  res.json({
    success: true
  })
})

module.exports = router
